package com.example.springbootpro.notic;

import com.example.springbootpro.exception.BusinessException;
import com.example.springbootpro.util.UserContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Aspect
@Component
@Order(1)
public class LoginAspect {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate ;

        @Around("@annotation(com.example.springbootpro.annotation.ResponseWrapper) || " +
            "@within(com.example.springbootpro.annotation.ResponseWrapper)")
            public Object wrapResponse(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        System.out.println("++++++参数");
        System.out.println(args.getClass());
        String username = "";
            String currentUser = UserContext.getCurrentUser();
            System.out.println(currentUser);
            String getRedis = null;
            try {
                getRedis = redisTemplate.opsForValue().get("USER_TOKEN:" + currentUser).toString();
            } catch (Exception e) {
                 throw new BusinessException("用户名和这个密码已过期请重新输入");
            }
            if (getRedis.isEmpty()||getRedis.equals("")) {
            throw new BusinessException("用户名和这个密码已过期请重新输入");
        }
        return  joinPoint.proceed();
    }
}
